﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
namespace HZ.Supervise.Web.Secondphase.ReportArea
{
    public partial class AreaofMonth : BasePage
    {
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        private bool hide = true;
        GroupByField groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortSource | GroupByField.AreaID;
        string sortField = "PurchaseTimeMonth asc";
        string append = "";

        string sortSource = "-1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误 
                {
                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }

        void BindList()
        {
            System.Data.DataTable dt;

            int t = 0;

            //从视图状态字典中获取查询的数据
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            //var model = new HZ.Data.QueryModel();

           // model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
               // model["SortSorce"] = droSort.SelectedValue;
                sortSource = droSort.SelectedValue;
            }

            //model["SortFields"] = this.droSortField.SelectedValue;
            if (sq.AreaID == Constants.CURRENT_DEPLOY_AREAID)
            {

                sq.AreaID = "";

            }
            dt = ie.ExcuteType(this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t, sq.Mid, sortSource, droSortField.SelectedValue, sq.AreaID);
            AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            gvwSortInfo.DataSource = dt;

            gvwSortInfo.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwSortInfo);

            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 7);


        }



        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份


                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8");
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "地区";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "入库率";
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }


        protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.AreaID = e.CommandArgument.ToString();
                    query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
                }
                catch { }
                Session[sessionid] = query;
                string url = "AreaCurrentGoodsList.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dt;

            int t = 0;

            //从视图状态字典中获取查询的数据
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            //var model = new HZ.Data.QueryModel();

           // model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
               // model["SortSorce"] = droSort.SelectedValue;
                sortSource = droSort.SelectedValue;
            }

           // model["SortFields"] = this.droSortField.SelectedValue;
            if (sq.AreaID == Constants.CURRENT_DEPLOY_AREAID)
            {

                sq.AreaID = "";

            }
            dt = ie.ExcuteType(1, 0, out t, sq.Mid, sortSource, droSortField.SelectedValue, sq.AreaID);
            AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwSortInfo);

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }
    }
}
